package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.FollowBehaviorDto;
import com.heima.behavior.pojos.ApFollowBehavior;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadLocal.ApUserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.temporal.ValueRange;
import java.util.Date;

@Service
public class ApFollowBehaviorServiceImpl implements ApFollowBehaviorService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 用户关注行为操作
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(FollowBehaviorDto dto) {
        //1.参数效验
        if (dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"作者id为空");
        }

        //2.判断当前是否用户登录还是游客登录
        ApUser apUser = ApUserThreadLocalUtil.getApUser();
        if (apUser==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer userId = apUser.getId();
        if (userId==0){
            //2.1如果是游客则不可以执行以下操作
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"游客身份,没有权限关注用户");
        }

        //3.先判断当前用户是否关注过该作者
        String followKey = "USER_FOLLOW_"+userId;
        String fansKey = "USER_FANS_"+dto.getAuthorId();
        Boolean flag = redisTemplate.opsForHash().hasKey(followKey, dto.getAuthorId().toString());

        //4.如果关注过并且此刻还在执行的是关注动作,则终止
        if (flag&&dto.getOperation()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"当前用户已经关注过该作者");
        }

        //5.如果关注过但是此刻正在执行的是取消关注动作,则取消redis和mongo中的数据
        if (flag&&dto.getOperation()==1){
            redisTemplate.opsForHash().delete(followKey,dto.getAuthorId().toString());
            redisTemplate.opsForHash().delete(fansKey,userId.toString());

            mongoTemplate.remove(Query.query(Criteria.where("userId").is(userId).and("followId").is(dto.getAuthorId())), ApFollowBehavior.class);
        }

        //6.如果没有关注过,则此刻正在执行关注的动作,则执行redis和mongo
        if (!flag&&dto.getOperation()==0){
            redisTemplate.opsForHash().put(followKey,dto.getAuthorId().toString(),"1");
            redisTemplate.opsForHash().put(fansKey,userId.toString(),"1");

            ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
            apFollowBehavior.setFollowId(dto.getAuthorId());
            apFollowBehavior.setUserId(Long.valueOf(userId));
            apFollowBehavior.setCreatedTime(new Date());
            apFollowBehavior.setArticleId(dto.getArticleId());
            mongoTemplate.save(apFollowBehavior);

            //4.如果没有关注过并且此刻还在执行的是取消关注动作,则终止
            if (flag&&dto.getOperation()==1){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"当前用户已经取消关注了该作者");
            }
        }
        //7.返回数据
        return ResponseResult.okResult("关注成功");
    }
}
